package cn.zebra.dev.mall.module.weixinPay.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.mall.module.weixinPay.service.WeXinPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "微信订单支付")
@RestController
@RequestMapping("/wx/orders")
public class WeXinPayController {

    @Autowired(required = false)
    private WeXinPayService weXinPayService;

    @PostMapping("/pay")
    @ApiOperation("微信预支付订单")
    public Result appPay(Long orderId, HttpServletRequest request) throws Exception {
        return Result.success(weXinPayService.prepay(orderId, request));
    }

    @GetMapping("/notify/url")
    @ApiOperation("微信支付通知")
    @Transactional(rollbackFor = Exception.class)
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
        weXinPayService.notifyUrl(request, response);
    }

    /**
     * @param ordersSn 订单编号
     * @return
     * @Description 查询订单的支付状态
     * @Date 9:50 2020/4/27
     **/
    @PostMapping("/status/query")
    @ApiOperation("查询订单支付状态")
    public Result findByPayStatus(@PathVariable String ordersSn) {
        Map<String, String> map = weXinPayService.findByPayStatus(ordersSn);
        return Result.success(map);
    }

}
